<?php
/**
 * Page Template
 *
 * Loaded automatically by index.php?main_page=account.<br />
 * Displays previous orders and options to change various Customer Account settings
 *
 * @package templateSystem
 * @copyright Copyright 2003-2005 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: tpl_account_default.php 4086 2006-08-07 02:06:18Z ajeh $
 *
 */
global $db;

require('tpl_users_default_functions.php');
//var_dump(get_necessary_customer_info($db, $_GET['nick']));
//header("Location: index.php?main_page=page_not_found");
/*****************************************process submitting*******************************************/
//var_dump($_POST);
$nickname = strtolower($_GET['nick']);

/**
 * @param $db
 */
function do_follow($db)
{
    $from = $_SESSION['customer_id'];
    $to = get_id_from_nickname($_GET['nick']);
    set_a_relationship_for_ids($db, $from, $to);
}/**
 * @param $db
 */
function do_un_follow($db)
{
    $from = $_SESSION['customer_id'];
    $to = get_id_from_nickname($_GET['nick']);
    del_a_relationship_of_ids($db, $from, $to);
}

/**
 * @param $nickname
 * @param $db
 */

if (isset($_POST["Upload"])) {
    check_and_upload_file($db);
} // Follow processing
elseif (isset($_POST['Follow'])) {
    do_follow($db);
} // Un_follow processing
elseif (isset($_POST['UnFollow'])) {
    do_un_follow($db);
} //set public or not
elseif (isset($_POST['public'])) {
    echo $_POST['isPublic'];
    $isPublic = $_POST['isPublic'] == "Yes" ? 1 : 0;
    $db->Execute("UPDATE " . TABLE_CUSTOMERS . " SET customers_public =" . $isPublic . " WHERE customers_nickname='" . $nickname . "'");

    if ($isPublic == 0) {
        //remove all relationship
        clear_all_relationship_of_nickname($nickname, $db);
    }
}

/******************************************generate form to upload or follow*****************************************/
echo "<hr>";
/**
 * @param $nickname
 * @param $db
 */
function build_follow_list_area($nickname, $db)
{
    if(!isset($_SESSION['customer_id']))
        return;
    if (!is_Followed($db, $_SESSION['customer_id'], get_id_from_nickname($nickname)))
        generate_follow_profile_form();
    else
        generate_un_follow_profile_form();
}

/*****************************************displaying page*******************************************/
$user = get_necessary_customer_info($db, $nickname);
//if (isset($_SESSION['customer_id']))
    if (isItMe()) {
        generate_upload_file_form();
        generate_public_form($db, $nickname);
        if ($user['image'] == null) {
//            echo "<h3>This user doesn't has photo </h3>";
            echo "<br><br>" . zen_image('images/user_photo_unavailable.jpg', '', 200, 200) . "<br><br>";
        }
        else{
            echo zen_image($user['image'], '', 200, 200) . "<br><br>";
        }

    } else //show profile accordingly to user's status
    {

//        var_dump($user);
        //Not existed
        if ($user == null) {
            header("HTTP/1.1 301 Moved Permanently");
            header("Location: index.php?main_page=page_not_found");
        } //Not public
        elseif (!$user['isPublic']) {
            echo "<h3>".TEXT_PROFILE_NOT_PUBLIC."</h3>";
            echo "<br><br>" . zen_image('images/user_photo_unavailable.jpg', '', 200, 200) . "<br><br>";
        } //Not have image
        elseif ($user['image'] == null) {
            echo "<h3>".TEXT_PROFILE_NOT_PHOTO."</h3>";
            echo "<br><br>" . zen_image('images/user_photo_unavailable.jpg', '', 200, 200) . "<br><br>";
        } //Have enough information
        else {
            echo zen_image($user['image'], '', 200, 200) . "<br><br>";
        }
        build_follow_list_area($nickname, $db);
    }

echo "<hr>".TEXT_USERS_FOLLOWING;
generate_list_of_friends(get_array_I_following(get_id_from_nickname($nickname)), 1);
echo "<hr>".TEXT_USERS_FOLLOWED;
generate_list_of_friends(get_array_users_followed_me(get_id_from_nickname($nickname)), 0);

echo "<hr>";

?>

<!--******************************************show history********************************************-->
<div class="centerColumn" id="accountDefault">

    <?php
    if ($reviews_split->number_of_rows > 0) {
        if ((PREV_NEXT_BAR_LOCATION == '1') || (PREV_NEXT_BAR_LOCATION == '3')) {
            ?>
            <div id="reviewsDefaultListingTopNumber"
                 class="navSplitPagesResult"><?php echo $reviews_split->display_count(TEXT_DISPLAY_NUMBER_OF_REVIEWS); ?></div>

            <div id="reviewsDefaultListingTopLinks"
                 class="navSplitPagesLinks"><?php echo TEXT_RESULT_PAGE . ' ' . $reviews_split->display_links(MAX_DISPLAY_PAGE_LINKS, zen_get_all_get_params(array('page', 'info', 'main_page'))); ?></div>

        <?php
        }

        $reviews = $db->Execute($reviews_split->sql_query);
        while (!$reviews->EOF) {
            ?>
            <hr/>

            <div
                class="smallProductImage back"><?php echo '<a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_INFO, 'products_id=' . $reviews->fields['products_id'] . '&reviews_id=' . $reviews->fields['reviews_id']) . '">' . zen_image(DIR_WS_IMAGES . $reviews->fields['products_image'], $reviews->fields['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a>'; ?></div>

            <div class="forward">
                <div
                    class="buttonRow"><?php echo '<a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_INFO, 'products_id=' . $reviews->fields['products_id'] . '&reviews_id=' . $reviews->fields['reviews_id']) . '">' . zen_image_button(BUTTON_IMAGE_READ_REVIEWS, BUTTON_READ_REVIEWS_ALT) . '</a>'; ?></div>
                <div
                    class="buttonRow"><?php echo '<a href="' . zen_href_link(zen_get_info_page($reviews->fields['products_id']), 'products_id=' . $reviews->fields['products_id']) . '">' . zen_image_button(BUTTON_IMAGE_GOTO_PROD_DETAILS, BUTTON_GOTO_PROD_DETAILS_ALT) . '</a>'; ?></div>
            </div>

            <h2><?php echo $reviews->fields['products_name']; ?></h2>

            <div
                class="rating"><?php echo zen_image(DIR_WS_TEMPLATE_IMAGES . 'stars_' . $reviews->fields['reviews_rating'] . '.gif', sprintf(TEXT_OF_5_STARS, $reviews->fields['reviews_rating'])), sprintf(TEXT_OF_5_STARS, $reviews->fields['reviews_rating']); ?></div>

            <div
                class="content"><?php echo zen_break_string(nl2br(zen_output_string_protected(stripslashes($reviews->fields['reviews_text']))), 60, '-<br />') . ((strlen($reviews->fields['reviews_text']) >= 100) ? '...' : ''); ?></div>

            <div
                class="bold"><?php echo sprintf(TEXT_REVIEW_DATE_ADDED, zen_date_short($reviews->fields['date_added'])); ?>
                &nbsp;<?php echo sprintf(TEXT_REVIEW_BY, zen_output_string_protected($reviews->fields['customers_name'])); ?></div>
            <br class="clearBoth"/>
            <?php
            $reviews->MoveNext();
        }
        ?>
    <?php
    } else {
        ?>
        <!--        <div id="reviewsDefaultNoReviews" class="content">--><?php //echo TEXT_NO_REVIEWS; ?><!--</div>-->
    <?php
    }
    ?>
    <?php
    if (($reviews_split->number_of_rows > 0) && ((PREV_NEXT_BAR_LOCATION == '2') || (PREV_NEXT_BAR_LOCATION == '3'))) {
        ?>
        <hr/>
        <div id="reviewsDefaultListingBottomNumber"
             class="navSplitPagesResult"><?php echo $reviews_split->display_count(TEXT_DISPLAY_NUMBER_OF_REVIEWS); ?></div>

        <div id="reviewsDefaultListingBottomLinks"
             class="navSplitPagesLinks"><?php echo TEXT_RESULT_PAGE . ' ' . $reviews_split->display_links(MAX_DISPLAY_PAGE_LINKS, zen_get_all_get_params(array('page', 'info', 'main_page'))); ?></div>
        <br class="clearBoth"/>
    <?php
    }
    //    var_dump($_SESSION);
    ?>

</div>